#include <QPlastiqueStyle>
#include "graphics/yigcolorkit.h"
#include "yigclock.h"
#include <QFont>

YigClock::YigClock(QWidget *parent) :
    QWidget(parent),
    mTimeSeconds(0),
    mTimeMinutes(0),
    mTimeHours(0)
{
    setGeometry(QRect(0, 0, 50, 25));
    //setWindowOpacity(0.77);

    QPlastiqueStyle yigStyle;
    QPalette yigPalette;
    yigPalette.setColor(QPalette::Window, YigColorKit::background2);
    yigPalette.setColor(QPalette::Button, YigColorKit::focus1);
    yigPalette.setColor(QPalette::ButtonText, YigColorKit::background2);
    setStyle(&yigStyle);
    setPalette(yigPalette);

    /*
    QPalette lcdPalette = mLCD.palette();
    lcdPalette.setColor(QPalette::Window, YigColorKit::background2);
    lcdPalette.setColor(QPalette::Button, YigColorKit::background2);
    lcdPalette.setColor(QPalette::ButtonText, YigColorKit::accent);
    lcdPalette.setColor(QPalette::Background, YigColorKit::background2);
    lcdPalette.setColor(QPalette::ButtonText, YigColorKit::accent);
    lcdPalette.setColor(QPalette::Highlight, YigColorKit::focus1);
    mLCD.setStyle(&yigStyle);
    mLCD.setPalette(lcdPalette);*/
    mLCD.setStyleSheet("QLineEdit{ background: #C19728; color: #000; border-radius: 0px}");
    mLCD.setGeometry(0, 0, 10, 10);
    mLCD.setFont(QFont("monaco", 12));
    mLCD.setAlignment(Qt::AlignCenter);
    mLCD.setMaximumSize(60, 30);
    mLCD.setReadOnly(true);
    mLCD.setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    mStartButton.setText(">");
    mStartButton.setGeometry(0, 0, 24, 12);
    mStartButton.setMinimumSize(24, 12);
    mStartButton.setMaximumSize(24, 12);
    mStartButton.setStyleSheet("QToolButton{ background: #222; color: #ddd; border-radius: 0px}"
                                "QToolButton::checked{ background: #bbb; color: #000; }"
                                "QToolButton::hover{ background: #bbb; color: #000; }");
    mStartButton.setCheckable(true);
    mStartButton.setFont(QFont("monaco", 6));

    //↩
    //"↻"
    // "ᐅ"
    mResetButton.setText("<");
    mResetButton.setGeometry(0, 0, 24, 12);
    mResetButton.setMinimumSize(24, 12);
    mResetButton.setMaximumSize(24, 12);
    mResetButton.setStyleSheet("QToolButton{ background: #222; color: #ddd; border-radius: 0px}"
                                "QToolButton::checked{ background: #bbb; color: #000; }"
                                "QToolButton::hover{ background: #bbb; color: #000; }");
    mResetButton.setFont(QFont("monaco", 6));

    mVerticalLayout.setGeometry(QRect(0, 0, 38, 25));
    mVerticalLayout.setSpacing(0);
    mVerticalLayout.setContentsMargins(0, 0, 0, 0);
    mVerticalLayout.addWidget(&mStartButton);
    mVerticalLayout.addWidget(&mResetButton);

    mHorizontalLayout.setGeometry(QRect(0, 0, 24, 12));
    mHorizontalLayout.setAlignment(Qt::AlignLeft);
    mHorizontalLayout.setSpacing(0);
    mHorizontalLayout.setContentsMargins(0, 0, 0, 0);
    mHorizontalLayout.addWidget(&mLCD);
    mHorizontalLayout.addLayout(&mVerticalLayout);
    setLayout(&mHorizontalLayout);

    QObject::connect(&mClockTimer, SIGNAL(timeout()), this, SLOT(tick()));
    QObject::connect(&mStartButton, SIGNAL(toggled(bool)), this, SLOT(toggleStartButton(bool)));
    QObject::connect(&mResetButton, SIGNAL(clicked()), this, SLOT(reset()));

    setClockState(RESET, false);
    show();
}

YigClock::~YigClock()
{
    mClockTimer.stop();
}

void YigClock::start()
{
    setClockState(START, true);
}

void YigClock::stop()
{
    setClockState(STOP, true);
}

void YigClock::reset()
{
    setClockState(RESET, true);
}

void YigClock::setClockState(ClockState state, bool updateNetwork)
{
    if(updateNetwork)
        emit clockStateChanged(state);

    switch(state)
    {
    case RESET:
        mTimeSeconds = 0;
        mTimeMinutes = 0;
        mTimeHours = 0;
        mLCD.setText(QTime(0, 0, 0).toString("mm:ss"));
        mClockTimer.stop();
        mStartButton.setText(">");
        mState = RESET;
        mStartButton.setChecked(false);
        clockStateChanged(mState);
        break;

    case START:
        mClockTimer.start(1000);
        mStartButton.setText("[]");
        mState = START;
        clockStateChanged(mState);
        break;

    case STOP:
        mClockTimer.stop();
        mStartButton.setText(">");
        mState = STOP;
        clockStateChanged(mState);
        break;
    }
}

void YigClock::toggleStartButton(bool checked)
{
    if(checked)
    {
        mStartButton.setText("[]");
        start();
    }

    else
    {
        mStartButton.setText(">");
        stop();
    }
}

void YigClock::tick()
{
    ++mTimeSeconds;
    if(mTimeSeconds >= 60)
    {
        mTimeSeconds = 0;
        ++mTimeMinutes;

        if(mTimeMinutes >= 60)
        {
            ++mTimeHours;
        }
    }

    mTime = QTime(mTimeHours, mTimeMinutes, mTimeSeconds);
    mLCD.setText(mTime.toString("mm:ss"));
}
